package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zzyl.vo.NursingProjectPlanVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description NursingPlanServiceImpl
 * @Author yuan yaming
 * @Date 2024-08-27
 */
@Service
@Transactional
public class NursingPlanServiceImpl implements NursingPlanService {
    private final NursingPlanMapper nursingPlanMapper;
    @Autowired
    public NursingPlanServiceImpl(NursingPlanMapper nursingPlanMapper) {
        this.nursingPlanMapper = nursingPlanMapper;
    }
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;
    /**
     * 查询所有护理计划
     * @return 护理计划Dto列表
     */
    @Override
    public List<NursingPlanVo> listAll() {
        return nursingPlanMapper.listAll();
    }

    /**
     * 根据名称和状态分页查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> listByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<List<NursingPlan>> lists = nursingPlanMapper.listByPage(name,status,pageNum,pageSize);
        return PageResponse.of(lists,NursingPlanVo.class);
    }

    /**
     * 根据ID查询计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        return nursingPlanMapper.getById(id);
    }

    /**
     * 增加护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto,NursingPlan.class);
        plan.setStatus(1);
        nursingPlanMapper.insert(plan);
        nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(plan.getId()));
        nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto,NursingPlan.class);
        NursingPlanVo id = getById(nursingPlanDto.getId());
        List<Long> ids = id.getProjectPlans().stream().map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        nursingProjectPlanService.deleteByIds(ids);
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())){
            nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(plan.getId()));
            nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
        }
        nursingPlanMapper.update(plan);
    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectPlanService.deleteByNursingPlanId(id);
        nursingPlanMapper.deleteById(id);
    }

    /**
     * 启用/禁用护理计划
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
